package com.whsxt.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
//import com.alipay.demo.trade.model.TradeStatus;
//import com.alipay.demo.trade.model.builder.AlipayTradePrecreateRequestBuilder;
//import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
//import com.alipay.demo.trade.service.AlipayTradeService;
import com.whsxt.config.AliPayProperties;
import com.whsxt.domain.Order;
import com.whsxt.domain.OrderSettlement;
import com.whsxt.feign.PayOrderFeign;
import com.whsxt.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-30 11:31
 * @Description:
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {


    @Resource
    private PayOrderFeign payOrderFeign;

    @Autowired
    private AlipayClient alipayClient;

//    @Autowired
//    private AlipayTradeService alipayTradeService;

    @Autowired
    private AliPayProperties aliPayProperties;


    /**
     * 调用支付，生成二维码
     *
     * @param orderSettlement
     * @return
     */
    @Override
    public String pay(OrderSettlement orderSettlement) {
        //拿到订单号，查询订单
        Order order = payOrderFeign.getOrderByOrderNum(orderSettlement.getOrderNumber());
        if (order == null) {
            throw new RuntimeException("订单错误");
        }
        //拿到订单名称，订单金额
        String prodName = order.getProdName();
        BigDecimal actualTotal = order.getActualTotal();
        //组装预支付参数
        //调用预支付接口
//        AlipayTradePrecreateRequestBuilder builder = new AlipayTradePrecreateRequestBuilder()
//                .setSubject("ego商城订单")
//                .setTotalAmount(actualTotal.toString())
//                .setOutTradeNo(orderSettlement.getOrderNumber())
//                .setUndiscountableAmount("0")
//                .setSellerId("2088621955520900")
//                .setBody(prodName)
//                .setOperatorId("001")
//                .setStoreId("shop001")
//                .setTimeoutExpress("120m")
//                .setNotifyUrl(aliPayProperties.getNotifyUrl());//支付宝服务器主动通知商户服务器里指定的页面http路径,根据需要设置

//        AlipayF2FPrecreateResult result = alipayTradeService.tradePrecreate(builder);
//
//        TradeStatus tradeStatus = result.getTradeStatus();
//        switch (tradeStatus) {
//            case SUCCESS:
//                //拿到预支付结果，也就是二维码
//                String qrCode = result.getResponse().getQrCode();
//                System.out.println(qrCode);
//                return qrCode;
//            case UNKNOWN:
//                throw new RuntimeException("订单创建失败");
//            case FAILED:
//                throw new RuntimeException("订单创建失败");
//            default:
//                throw new RuntimeException("订单创建失败");
//        }
        return null;
    }






    /**
     * 支付成功后修改订单状态
     *
     * @param orderNum
     */
    @Override
    public void paySuccessChangeStatus(String orderNum) {
        //远程调用修改订单状态
        log.info("开始了");
        String sey = "34324";
        payOrderFeign.changeStatusByOrderNum(orderNum);
    }









    /**
     * 验证签名是否正确
     *
     * @param map
     * @return
     */
    @Override
    public Boolean checkRes2(Map<String, String> map) {
        boolean flag = true;
        try {
            flag = AlipaySignature.rsaCheckV1(map,
                    aliPayProperties.getAlipayPublicKey(),
                    "UTF-8",
                    aliPayProperties.getSignType());
        } catch (AlipayApiException e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }





}
